<?php

namespace App\Models\goods;

use App\Models\base\traits\ImgTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


/**
 * App\Models\goods\Goods
 *
 * @property int $id
 * @property string $img 商品主图
 * @property string $spu_id 商品编号
 * @property string $name 商品名
 * @property int $cate_id 商品分类(3级ID)
 * @property int $cate1_id 1级分类
 * @property int $cate2_id 2级分类ID
 * @property int $brand_id 商品品牌ID
 * @property int $sale_num 销量
 * @property float $price 商品现价
 * @property float $old_price 商品原价
 * @property int $status 商品状态  1=>上架   0=>下架
 * @property int $stock 商品库存
 * @property int $sort 排序
 * @property int $is_recommend 是否推荐
 * @property int $is_pt 是否为拼团商品
 * @property int $is_xql 是否加入秒杀星期六
 * @property int $is_sr 是否加入生日大礼包
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \App\Models\goods\GoodsCate $cate
 * @property-read \App\Models\goods\GoodsCate $cate1
 * @property-read \App\Models\goods\GoodsDetail $detail
 * @property-read mixed $header_img
 * @property-read mixed $icon
 * @property mixed $icons
 * @property-read mixed $img2
 * @property mixed $imgs
 * @property mixed $parameter
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\goods\OrderGroup[] $order_groups
 * @property-read int|null $order_groups_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\goods\GoodsSpec[] $specs
 * @property-read int|null $specs_count
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\goods\Goods onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereBrandId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereCate1Id($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereCate2Id($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereCateId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereIsPt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereIsRecommend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereIsSr($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereIsXql($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereOldPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereSaleNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereSpuId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereStock($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\goods\Goods withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\goods\Goods withoutTrashed()
 * @mixin \Eloquent
 * @property string|null $alias 内部简称(用于打印小票)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Goods whereAlias($value)
 */
class Goods extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at','created_at','updated_at'];

    use ImgTrait;

    protected $fillable = [
        'sku',
        'name',
        'cate_id',
//        'brand_id',
        'sale_num',
        'type',
        'price',
        'old_price',
        'stock',
        'img',
        'sort',
        'status',
        'cate1_id',
        'is_recommend',
        'alias'
    ];

    public function cate()
    {
        return $this->hasOne(GoodsCate::class, 'id','cate_id');
    }

    public function cate1()
    {
        return $this->hasOne(GoodsCate::class, 'id','cate1_id');
    }

    public function detail()
    {
        return $this->hasOne(GoodsDetail::class,'goods_id');
    }

    public function specs()
    {
        return $this->hasMany(GoodsSpec::class, 'goods_id');
    }

    public function order_groups()
    {
        return $this->hasMany(OrderGroup::class, 'goods_id');
    }


    public function getImg2Attribute($value)
    {
        return $value ? starts_with($value, 'http') ? $value : config('filesystems.disks.admin.url').$value : '';
    }

    public function setCateIdAttribute($value)
    {

        if (!is_numeric($value)){
            $value = GoodsCate::whereName($value)->value('id');
        }

        $this->attributes['cate_id'] = $value;
    }

    public function setCate1IdAttribute($value)
    {

        if (!is_numeric($value)){
            $value = GoodsCate::whereName($value)->value('id');
        }

        $this->attributes['cate1_id'] = $value;
    }

    public function setCate2IdAttribute($value)
    {

        if (!is_numeric($value)){
            $value = GoodsCate::whereName($value)->value('id');
        }

        $this->attributes['cate2_id'] = $value;
    }

}
